﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using EnCor.Security;
using EnCor.Logging;
using System.Xml;
using System.IO;

namespace EnCor.Config
{
    internal class EnCorConfig : ConfigurationSection
    {
        private const string STR_EnCor = "enCor";
        private const string STR_Modules = "modules";
        private const string STR_Security = "security";
        private const string STR_Logging = "logging";
        private const string STR_Hosting = "hosting";
        private const string STR_EnCorConfigFile = "EnCor.config";

        internal static EnCorConfig GetConfig()
        {
            object section = ConfigurationManager.GetSection(STR_EnCor);
            if (section != null)
            {
                return (EnCorConfig)section;
            }

            EnCorConfig config = new EnCorConfig();
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

            using (XmlReader reader = new XmlTextReader(Path.Combine(baseDirectory, STR_EnCorConfigFile)))
            {
                reader.ReadStartElement(STR_EnCor);
                config.DeserializeElement(reader, false);
                return config;
            }
        }


        [ConfigurationProperty(STR_Modules)]
        public ModuleConfigCollection Modules
        {
            get
            {
                return (ModuleConfigCollection)this[STR_Modules];
            }
        }

        [ConfigurationProperty(STR_Security)]
        public SecurityConfig Security
        {
            get
            {
                return (SecurityConfig)this[STR_Security];
            }
        }

        [ConfigurationProperty(STR_Logging)]
        public LoggingConfig Logging
        {
            get
            {
                return (LoggingConfig)this[STR_Logging];
            }
        }

        [ConfigurationProperty(STR_Hosting)]
        public HostingConfig Hosting
        {
            get
            {
                return (HostingConfig)this[STR_Hosting];
            }
        }
    }
}
